package com.example.mansystem.Activiti;

import com.example.mansystem.entity.InstrumentCaCheDO;
import com.example.mansystem.entity.InstrumentDO;
import com.example.mansystem.entity.InstrumentExamineDO;
import com.example.mansystem.mapper.InstrumentEditCacheMapper;
import com.example.mansystem.mapper.InstrumentExamineMapper;
import com.example.mansystem.mapper.InstrumentMapper;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.io.Serializable;

@Component("myServiceTask")
public class MyServiceTask implements Serializable, JavaDelegate {

    private static InstrumentExamineMapper instrumentExamineMapper;
    private static InstrumentMapper instrumentMapper;
    private static InstrumentEditCacheMapper instrumentEditCacheMapper;

    private static final long serialVersionUID = 1L;

    @Autowired
    public  void setInstrumentExamineMapper(InstrumentExamineMapper instrumentExamineMapper)
    {
        MyServiceTask.instrumentExamineMapper=instrumentExamineMapper;
    }

    @Autowired
    public  void setInstrumentMapper(InstrumentMapper instrumentMapper)
    {
        MyServiceTask.instrumentMapper=instrumentMapper;
    }

    @Autowired
    public  void setInstrumentEditCacheMapper(InstrumentEditCacheMapper instrumentMapper)
    {
        MyServiceTask.instrumentEditCacheMapper=instrumentMapper;
    }

    @Override
    public void execute(DelegateExecution execution) {
        int type =(int)execution.getVariable("type");
        int pass =(int)execution.getVariable("pass");
        if(type==1)
        {
            int examId =(int)execution.getVariable("examId");

            InstrumentExamineDO instrumentExamineDO = instrumentExamineMapper.selectById(examId);
            int YQBH = instrumentExamineDO.getYQBH();
            if(pass==1)
            {
                instrumentExamineDO.setState(1);
                InstrumentDO instrumentDO = instrumentMapper.selectById(YQBH);
                instrumentDO.setState(1);
                instrumentMapper.updateById(instrumentDO);
                instrumentExamineMapper.updateById(instrumentExamineDO);
            }
            else if(pass==0)
            {
                instrumentExamineDO.setState(-1);
                instrumentExamineMapper.updateById(instrumentExamineDO);
            }
        }
        else if(type ==2)
        {
            int examId =(int)execution.getVariable("examId");

            InstrumentExamineDO instrumentExamineDO = instrumentExamineMapper.selectById(examId);
            int YQBH = instrumentExamineDO.getYQBH();
            if(pass==1)
            {
                InstrumentCaCheDO instrumentCaCheDO = instrumentEditCacheMapper.selectById(YQBH);
                InstrumentDO instrumentDO = instrumentMapper.selectById(YQBH);
                BeanUtils.copyProperties(instrumentCaCheDO,instrumentDO);
                instrumentMapper.updateById(instrumentDO);
                instrumentExamineDO.setState(1);
                instrumentExamineMapper.updateById(instrumentExamineDO);
            }
            else if(pass==0)
            {
                instrumentEditCacheMapper.deleteById(YQBH);
                instrumentExamineDO.setState(-1);
                instrumentExamineMapper.updateById(instrumentExamineDO);
            }
        }
        else if(type ==3)
        {
            int examId =(int)execution.getVariable("examId");

            InstrumentExamineDO instrumentExamineDO = instrumentExamineMapper.selectById(examId);
            int YQBH = instrumentExamineDO.getYQBH();
            if(pass==1)
            {
                instrumentExamineDO.setState(1);
                instrumentExamineMapper.updateById(instrumentExamineDO);
                instrumentMapper.deleteById(YQBH);
            }
            else if(pass==0)
            {
                instrumentExamineDO.setState(-1);
                instrumentExamineMapper.updateById(instrumentExamineDO);
            }
        }

    }
}